package com.baizhi.controller;

import com.alibaba.fastjson.JSONObject;
import com.baizhi.entities.Product;
import com.baizhi.feignclient.ProductClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class CategoryController {

    @Autowired
    ProductClient productClient;

    @GetMapping("/category")
    public String category(){
//        String result = productClient.test("纸巾", 12);
//        String result = productClient.test("连衣裙");
//        String result = productClient.test(new Product("T恤",199));
//        String result = productClient.test(new String[]{"短袖","裤子"});
        String result = productClient.test5(new String[]{"21","22"});
//        Product result = productClient.getProduct(6);
//        String result = productClient.product();
        return  "category OK + :" + result.toString();
    }

    @GetMapping("/category2")
    public /*Map<String, Object>*/String category2(){

        String productPage = productClient.getProductPage(10, 1, 21);
        JSONObject jsonObject = JSONObject.parseObject(productPage);
        log.info("total:{}",jsonObject.get("total"));
        List<Product> rows = JSONObject.parseArray(jsonObject.get("rows").toString(), Product.class);
        //Map<String, Object> productPage = productClient.getProductPage(10, 1, 21);
        //System.out.println(productPage.get("total"));
        /*//报错
        List<Product> rows = (List<Product>)(productPage.get("rows"));
        rows.forEach(product -> {
            log.info("product:{}",product.toString());
        });*/
        //这样可以
        //String arrayStr = JSONObject.toJSONString(productPage.get("rows"));
        //List<Product> rows = JSONObject.parseArray(arrayStr, Product.class);

        rows.forEach(product -> {
            log.info("product:{}",product.toString());
        });

        return productPage;
    }

}
